package com.chen.gui.component.button;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JToggleButton;

import com.chen.qq.Context;

/**
 * 
 * @author Chen
 * 
 */
public class ToolButton extends JToggleButton {

    private static final long serialVersionUID = 85383556468916879L;

    public ToolButton(String text, String iconName) {
        setVerticalTextPosition(JButton.BOTTOM);
        setHorizontalTextPosition(JButton.CENTER);
        setOpaque(false);
        setContentAreaFilled(false);
        setBorder(BorderFactory.createEmptyBorder(0, 15, 0, 15));
        setIconTextGap(0);
        setFocusPainted(false);
        setRolloverEnabled(true);

        this.setForeground(Color.white);
        this.setText(text);
        this.setIcon(Context.getIcon(iconName));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        if (getModel().isSelected()) {
            g2d.setStroke(new BasicStroke(0.7f));
            g2d.setColor(new Color(220, 220, 220));
            g2d.drawRoundRect(4, 4, getWidth() - 8, getHeight() - 8, 5, 5);
            g2d.setColor(Color.DARK_GRAY);
            g2d.drawRoundRect(3, 3, getWidth() - 6, getHeight() - 6, 5, 5);
        } else if (getModel().isRollover()) {
            g2d.setColor(Color.LIGHT_GRAY);
            g2d.drawRoundRect(4, 4, getWidth() - 8, getHeight() - 8, 5, 5);
            g2d.setColor(Color.gray);
            g2d.drawRoundRect(3, 3, getWidth() - 6, getHeight() - 6, 5, 5);
        }
    }
}
